package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.FriendCircleBean;
import mamikeji.liaowa.bean.Image;
import mamikeji.liaowa.ui.activity.circle.PreviewCircleImgActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;
import mamikeji.liaowa.widget.ninegrid.CustomImageView;
import mamikeji.liaowa.widget.ninegrid.NineGridlayout;
import mamikeji.liaowa.widget.ninegrid.ScreenTools;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class Main_dynamic_selAdapter extends BaseAdapter {
    List<FriendCircleBean.DataEntity> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;

    public Main_dynamic_selAdapter() {
    }

    public Main_dynamic_selAdapter(List<FriendCircleBean.DataEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onZanClick(int fc_id, int position);
        void onGuanZhuClick(String followusercode, int position);
        void onMSGClick(int fc_id, int position);
    }

    public void setList(List<FriendCircleBean.DataEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_main_dynamic_sel, null);
            viewHolder = new ViewHolder();

            viewHolder.imageView_dynamic_zan_sel = (ImageView) convertView.findViewById(R.id.imageView_dynamic_zan_sel);
            viewHolder.linearLayout_dynamic_zannum = (LinearLayout) convertView.findViewById(R.id.linearLayout_dynamic_zannum);
            viewHolder.linearLayout_dynamic_recommendnum = (LinearLayout) convertView.findViewById(R.id.linearLayout_dynamic_recommendnum);
            viewHolder.textView_dynamic_zannum_sel = (TextView) convertView.findViewById(R.id.textView_dynamic_zannum_sel);
            viewHolder.textView_dynamic_recommend_sel = (TextView) convertView.findViewById(R.id.textView_dynamic_recommend_sel);
            viewHolder.textView_dynamic_nickname_sel = (TextView) convertView.findViewById(R.id.textView_dynamic_nickname_sel);
            viewHolder.textView_dynamic_creattime_sel = (TextView) convertView.findViewById(R.id.textView_dynamic_creattime_sel);
            viewHolder.textView_dynamic_time_sel = (TextView) convertView.findViewById(R.id.textView_dynamic_time_sel);
            viewHolder.textView_dynamic_details_content = (TextView) convertView.findViewById(R.id.textView_dynamic_details_content);
            viewHolder.imageView_dynamic_titleimg_sel = (SimpleDraweeView) convertView.findViewById(R.id.imageView_dynamic_titleimg_sel);
            viewHolder.linearLayout_dynamic_guanzhu_sel = (LinearLayout) convertView.findViewById(R.id.linearLayout_dynamic_guanzhu_sel);
            viewHolder.imageView_dynamic_guanzhu_sel = (ImageView) convertView.findViewById(R.id.imageView_dynamic_guanzhu_sel);
            viewHolder.textView_dynamic_guanzhu_sel = (TextView) convertView.findViewById(R.id.textView_dynamic_guanzhu_sel);

            viewHolder.ivMore_sel = (NineGridlayout) convertView.findViewById(R.id.iv_ngrid_layout_sel);
            viewHolder.ivOne_sel = (CustomImageView) convertView.findViewById(R.id.iv_oneimage_sel);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final FriendCircleBean.DataEntity dataEntity = mList.get(position);
        String[] split2 = new String[]{};
        if (!dataEntity.getImgurl().equals("") && !dataEntity.getImgurl().equals("null") && dataEntity.getImgurl() != null){
            split2 = dataEntity.getImgurl().split(",");
        }

        viewHolder.linearLayout_dynamic_guanzhu_sel.setVisibility(ForPreference.getString(
                ForContents.USER_CODE).equals(dataEntity.getUsercode()) ? View.GONE : View.VISIBLE);
        viewHolder.imageView_dynamic_guanzhu_sel.setVisibility( dataEntity.getFollow()==2 ?
                View.VISIBLE : View.GONE);
        viewHolder.linearLayout_dynamic_guanzhu_sel.setBackgroundResource(dataEntity.getFollow()==2 ?
                R.drawable.shape_momcircle_guanzhu_btn_bg_sel : R.color.color_ffffff);
        switch (dataEntity.getFollow()){
            case 1:
                viewHolder.textView_dynamic_guanzhu_sel.setTextColor(context.getResources().getColor(R.color.color_text_333));
                viewHolder.textView_dynamic_guanzhu_sel.setText( R.string.momcircle_guanzhu_1);
                break;
            case 2:
                viewHolder.textView_dynamic_guanzhu_sel.setTextColor(context.getResources().getColor(R.color.color_ffffff));
                viewHolder.textView_dynamic_guanzhu_sel.setText( R.string.momcircle_guanzhu_2);
                break;
            case 3:
                viewHolder.textView_dynamic_guanzhu_sel.setTextColor(context.getResources().getColor(R.color.color_text_333));
                viewHolder.textView_dynamic_guanzhu_sel.setText( R.string.momcircle_guanzhu_3);
                break;
        }
        viewHolder.imageView_dynamic_zan_sel.setImageResource(dataEntity.getPraisestate() == 1 ? R.drawable.icon_dianzan_h : R.drawable.icon_dianzan_n);
        viewHolder.textView_dynamic_zannum_sel.setText(dataEntity.getPraisenumber()+"");
        viewHolder.textView_dynamic_recommend_sel.setText("0");
        viewHolder.textView_dynamic_time_sel.setText(dataEntity.getBabyage());
        viewHolder.textView_dynamic_nickname_sel.setText(dataEntity.getName());
        viewHolder.textView_dynamic_creattime_sel.setText(mistiming(dataEntity.getPublisdate()));

        if (dataEntity.getContent() == null || dataEntity.getContent().equals("")){
            viewHolder.textView_dynamic_details_content.setVisibility(View.GONE);
        }else {
            viewHolder.textView_dynamic_details_content.setVisibility(View.VISIBLE);
            viewHolder.textView_dynamic_details_content.setText(dataEntity.getContent());

            if (dataEntity.getContent().length() >= 20){
                viewHolder.textView_dynamic_details_content.setLines(2);
                viewHolder.textView_dynamic_details_content.setEllipsize(TextUtils.TruncateAt.END);
            }
        }

        viewHolder.imageView_dynamic_titleimg_sel.setImageURI(Uri.parse(dataEntity.getAvatar()));

        if (split2.length == 0) {
            viewHolder.ivMore_sel.setVisibility(View.GONE);
            viewHolder.ivOne_sel.setVisibility(View.GONE);
        } else if (split2.length == 1) {
            viewHolder.ivMore_sel.setVisibility(View.GONE);
            viewHolder.ivOne_sel.setVisibility(View.VISIBLE);

            handlerOneImage(viewHolder, new Image(split2[0],250,250));
        } else {
            viewHolder.ivMore_sel.setVisibility(View.VISIBLE);
            viewHolder.ivOne_sel.setVisibility(View.GONE);

            List<String> lsists = new ArrayList<>();
            for (int i = 0 ;i<split2.length;i++){
                lsists.add(split2[i]);
            }

            viewHolder.ivMore_sel.setImagesData(lsists);
        }

        final String[] finalSplit = split2;
        viewHolder.ivMore_sel.setOnItemClickListener(new NineGridlayout.OnItemClickListener() {
            @Override
            public void onMultipleImgClick(int position) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i< finalSplit.length; i++){
                    stringList.add(finalSplit[i]);
                }

                Intent intent = new Intent(context,PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position",position);
                context.startActivity(intent);
            }
        });
        viewHolder.ivOne_sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> stringList = new ArrayList<String>();
                for (int i = 0; i< finalSplit.length; i++){
                    stringList.add(finalSplit[i]);
                }

                Intent intent = new Intent(context,PreviewCircleImgActivity.class);
                intent.putStringArrayListExtra("urlList", stringList);
                intent.putExtra("position",1);
                context.startActivity(intent);
            }
        });

        viewHolder.linearLayout_dynamic_zannum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onZanClick(dataEntity.getFcid(), position);
            }
        });

        viewHolder.linearLayout_dynamic_guanzhu_sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dataEntity.getFollow()==2){
                    mOnItemClickListener.onGuanZhuClick(dataEntity.getUsercode(), position);
                }
            }
        });

        viewHolder.imageView_dynamic_titleimg_sel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onMSGClick(dataEntity.getFcid(), position);
            }
        });
        return convertView;
    }

    public class ViewHolder {
        ImageView imageView_dynamic_zan_sel;
        TextView textView_dynamic_zannum_sel;
        TextView textView_dynamic_recommend_sel;
        TextView textView_dynamic_time_sel;
        TextView textView_dynamic_nickname_sel;
        TextView textView_dynamic_creattime_sel;
        TextView textView_dynamic_details_content;
        SimpleDraweeView imageView_dynamic_titleimg_sel;
        NineGridlayout ivMore_sel;
        CustomImageView ivOne_sel;
        LinearLayout linearLayout_dynamic_zannum;
        LinearLayout linearLayout_dynamic_recommendnum;
        LinearLayout linearLayout_dynamic_guanzhu_sel;
        ImageView imageView_dynamic_guanzhu_sel;
        TextView textView_dynamic_guanzhu_sel;
    }

    //判断时间差：新闻发布时间与当前系统时间的差值：如果小于60分钟换算成分钟；大于60分钟，换算成小时；大于24小时，换算成天
    public String mistiming(String time) {
        String date1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

        SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date beginTime = null;
        try {
            beginTime = CurrentTime.parse(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date endTime = null;
        try {
            endTime = CurrentTime.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //进行判断
        Long shijianCha = beginTime.getTime() - endTime.getTime();
        int count = 0;
        if (shijianCha > 24 * 60 * 60 * 1000) {
            count = (int) (shijianCha / (24 * 60 * 60 * 1000));
            return count + "天前";
        } else if (shijianCha > 60 * 60 * 1000) {
            count = (int) (shijianCha / (60 * 60 * 1000));
            return count + "小时前";
        } else {
            count = (int) (shijianCha / (60 * 1000));
            return count + "分钟前";
        }
    }

    private void handlerOneImage(ViewHolder viewHolder, Image image) {
        int totalWidth;
        int imageWidth;
        int imageHeight;
        ScreenTools screentools = ScreenTools.instance(context);
        totalWidth = screentools.getScreenWidth() - screentools.dip2px(80);
        imageWidth = screentools.dip2px(image.getWidth());
        imageHeight = screentools.dip2px(image.getHeight());
        if (image.getWidth() <= image.getHeight()) {
            if (imageHeight > totalWidth) {
                imageHeight = totalWidth;
                imageWidth = (imageHeight * image.getWidth()) / image.getHeight();
            }
        } else {
            if (imageWidth > totalWidth) {
                imageWidth = totalWidth;
                imageHeight = (imageWidth * image.getHeight()) / image.getWidth();
            }
        }
        ViewGroup.LayoutParams layoutparams = viewHolder.ivOne_sel.getLayoutParams();
        layoutparams.height = imageHeight;
        layoutparams.width = imageWidth;
        viewHolder.ivOne_sel.setLayoutParams(layoutparams);
        viewHolder.ivOne_sel.setClickable(true);
        viewHolder.ivOne_sel.setScaleType(ImageView.ScaleType.FIT_XY);
        viewHolder.ivOne_sel.setImageUrl(image.getUrl());

    }
}
